package com.ztjava.auth.controller;


import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Duang;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Ret;
import com.ztjava.auth.model.AuthUser;
import com.ztjava.auth.service.AuthService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.annotation.CustomTemplate;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.ext.auth.tool.AuthTool;
import com.ztjava.plugin.event.EventKit;
import com.ztjava.plugin.event.EventTag;
import com.ztjava.plugin.event.core.RequestEvent;

/***
 * 因后台功能无需过多个性化，jsub自定义了。
 * @author Administrator
 *
 */
@ControllerClass(path="",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class PortalController extends BaseController{
	private static AuthService authService = Duang.duang(AuthService.class);
	/***
	 * admin客户端首页
	 */
	@UserMethod
	@CustomTemplate("index.html")
	public void index(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Map<String,JSONObject> menu = authService.getUserMenu(authSession, getRequest(),AuthTool.cookie);
		setAttr("menu", menu);
		
		renderCustom();
	}
	/***
	 * 更新所有配置的缓存
	 */
	@UserMethod
	public void reloadCache(){
		List<Ret> retList = EventKit.postRemote(EventTag.RELOAD_CACHE, new RequestEvent("ALL"));
		renderSuccess(retList.size()+"个系统刷新了缓存！");
	}

	@UserMethod
	public void editPassword(){
		render("editPassword.html");
	}

	@UserMethod
	public void doEditPassword(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		String olderpassword = getPara("olderpassword","");
		String newpassword = getPara("newpassword","");
		String repassword = getPara("repassword","");
		if(olderpassword.equals("")||newpassword.equals("")||repassword.equals("")){
			renderError("数据不完整！");
			return;
		}
		AuthUser authUser = AuthUser.dao.findById(authSession.getId());
		if(!authUser.getStr("password").equals(HashKit.md5(olderpassword).toUpperCase())){
			renderError("原密码不正确！");
			return;
		}
		if(!newpassword.equals(repassword)){
			renderError("确认密码不正确！");
			return;
		}
		authUser.set("password", HashKit.md5(newpassword).toUpperCase()).update();
		renderSuccess();
	}
	/***
	 * 图片验证码
	 */
	public void captcha(){
		renderCaptcha();
	}

}
